<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('recruitment_warnings', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联信息
            $table->unsignedBigInteger('department_position_id');
            $table->unsignedBigInteger('department_id');
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id');
            $table->string('position_name', 100)->nullable();

            // 预警信息
            $table->string('warning_type', 50); // understaffing, overstaffing, vacancy
            $table->string('warning_level', 20)->default('medium'); // low, medium, high, critical
            $table->string('warning_title', 200); // 预警标题
            $table->text('warning_content'); // 预警内容

            // 预警数据
            $table->integer('standard_headcount')->nullable(); // 标准人数
            $table->integer('current_headcount')->nullable(); // 当前人数
            $table->integer('vacancy_count')->nullable(); // 空缺数量
            $table->decimal('occupancy_rate', 5, 2)->nullable(); // 占用率
            $table->integer('warning_threshold')->nullable(); // 预警阈值

            // 触发条件
            $table->string('trigger_condition', 100)->nullable(); // 触发条件描述
            $table->json('trigger_data')->nullable(); // 触发时的详细数据

            // 状态管理
            $table->string('status', 20)->default('active'); // active, acknowledged, resolved, dismissed
            $table->datetime('acknowledged_at')->nullable(); // 确认时间
            $table->string('acknowledged_by', 100)->nullable(); // 确认人
            $table->datetime('resolved_at')->nullable(); // 解决时间
            $table->string('resolved_by', 100)->nullable(); // 解决人

            // 解决措施
            $table->text('resolution_measures')->nullable(); // 解决措施
            $table->string('resolution_type', 50)->nullable(); // recruitment, transfer, adjustment
            $table->text('resolution_notes')->nullable(); // 解决备注

            // 跟进信息
            $table->datetime('next_follow_up_date')->nullable(); // 下次跟进日期
            $table->text('follow_up_notes')->nullable(); // 跟进备注
            $table->integer('follow_up_count')->default(0); // 跟进次数

            // 自动处理
            $table->boolean('auto_generated')->default(true); // 是否自动生成
            $table->string('generation_rule', 100)->nullable(); // 生成规则

            // 优先级和紧急程度
            $table->integer('priority')->default(1); // 1-5，1最低，5最高
            $table->boolean('requires_immediate_action')->default(false); // 需要立即处理

            // 通知信息
            $table->boolean('notification_sent')->default(false); // 是否已发送通知
            $table->datetime('notification_sent_at')->nullable(); // 通知发送时间
            $table->json('notification_recipients')->nullable(); // 通知接收人

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['department_position_id', 'warning_type'], 'rw_dp_wtype_idx');
            $table->index(['warning_type', 'status'], 'rw_wtype_status_idx');
            $table->index(['warning_level'], 'rw_level_idx');
            $table->index(['status'], 'rw_status_idx');
            $table->index(['priority'], 'rw_priority_idx');
            $table->index(['requires_immediate_action'], 'rw_immediate_idx');
            $table->index(['next_follow_up_date'], 'rw_followup_idx');

            // 外键约束
            $table->foreign('department_position_id')->references('id')->on('department_positions')->onDelete('cascade');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('recruitment_warnings');
    }
};
